package com.niit.mobileDevide.screenon;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class ScreenOnDriver {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "User Behavior Analysis by Screen On Time");

        job.setJarByClass(ScreenOnDriver.class);
        job.setMapperClass(ScreenOnMapper.class);
        job.setReducerClass(ScreenOnReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(ScreenOnBean.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(ScreenOnBean.class);

        // 设置reducer的数量
        job.setNumReduceTasks(1); // 如果需要多个reducer可以根据实际需求调整

        // 配置本次作业的输入数据路径和输出数据路径
        Path inputPath = new Path("input/user_behavior_dataset.csv");
        Path outputPath = new Path("output/screen_on");

        // 默认组件 TextInputFormat 和 TextOutputFormat
        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job, outputPath);

        // 删除已存在的输出目录
        if (outputPath.getFileSystem(conf).exists(outputPath)) {
            outputPath.getFileSystem(conf).delete(outputPath, true);
        }

        // 启动作业
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}